#!python3.2
#-*- coding: utf-8 -*-
"""
ini2dict -- save an ini file as a pickled Python dictionary
"""
epilog = """\
This script has been written to convert Karrigell translation.ini to
a pickled Python dictionary, but will work with any ini file. 

If the ini file is:
[section1]
option1=value1
option2=value2
[section2]
option1=value1
option2=value2
[section3]
option1=value1
option2=value2

The dictionary has the form:

{"section1":{"option1":"value1", "option2":"value2" ...},
 "section2":{"option1":"value1", "option2":"value2" ...},
 "section3":{"option1":"value1", "option2":"value2" ...}}
 
"""

import sys, os
import configparser
import pickle
import argparse

#Process command line
parser = argparse.ArgumentParser(
    description=__doc__,
    epilog=epilog,
    formatter_class=argparse.RawDescriptionHelpFormatter
    )
parser.add_argument('inipath', default=None,
    help='Path to input ini file')
parser.add_argument('picklepath', default=None,
    help='Path to output pickle file')
parser.add_argument('-e', '--encoding', required=False, default='utf-8',
    help='Encoding to use (default: UTF-8)')

args = parser.parse_args()

def load_ini(inifile,encoding='utf-8'):
    ini = configparser.ConfigParser()
    ini.read([inifile],encoding=encoding)
    translations = {}
    for section in ini.sections():
        translations[section] = {}
        for option in ini.options(section):
            translations[section][option]=ini.get(section,option)
    del ini
    return translations

def main():
    d = load_ini(args.inipath, encoding=args.encoding)
    with open(args.picklepath, 'wb') as f:
        pickle.dump(d, f)

if __name__ == '__main__':
    main()